<?php
namespace app\admin\controller;
use think\Db;

class Log extends Base
{
    public function index(){
		if(request()->isAjax()){
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
			if($param['searchText'] == ''){
				$whereText_username = [];
				$whereText_nodename = [];
			}else{
				$whereText_username = [
					'user_name' => ['like','%'.$param['searchText'].'%']
				];
				$whereText_nodename = [
					'url_name' => ['like','%'.$param['searchText'].'%']
				];
			}
			if($param['searchDate'] == ''){
				$whereDate_s = [];
				$whereDate_e = [];
			}else{
				$whereDate_s = [
					'c_time' => ['>',strtotime($param['searchDate'])]
				];
				$whereDate_e = [
					'c_time' => ['<=',(strtotime($param['searchDate']) + 24 *3600)]
				];
			}
            $selectResult = DB::name('log')->order('c_time desc')
				->where($whereDate_s)
				->where($whereDate_e)
				->where(function($query)use($whereText_username,$whereText_nodename){
					$query->whereOr($whereText_username);
					$query->whereOr($whereText_nodename);
				})
				->limit($offset, $limit)->select();
            foreach($selectResult as $key=>$vo){
                $selectResult[$key]['c_time'] = date("Y-m-d H:i:s",$vo['c_time']);
            }
            $return['total'] = DB::name('log')
            	->where($whereDate_s)
				->where($whereDate_e)
				->where(function($query)use($whereText_username,$whereText_nodename){
					$query->whereOr($whereText_username);
					$query->whereOr($whereText_nodename);
				})->count();  // 总数据
            $return['rows'] = $selectResult;
            return [
				'code' => 1,
				'msg' => '表格加载成功',
				'data' => $return
			];
        }
        return $this->fetch();
    }
}
